<?php
class Default_Model_index extends Zend_Db_Table{

	public function get_cat($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('category', array('id','title')) 
		          ->where('published = ?', '1')
		          ->where('section_id = ?', $id);
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
	
	public function get_solution($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('category_solution', array('id','title')) 
		          ->where('published = ?', '1')
		          ->where('section_id = ?', $id);
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
	
	public function get_product($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('category_product', array('id','title')) 
		          ->where('published = ?', '1')
		          ->where('section_product_id = ?', $id);
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
	
	public function get_details($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article') 
		          ->where('id = ?',$id) ;
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
	
	public function get_cat_details($cat)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article',array('id','title')) 
		          ->where('cat_id = ?',$cat)
		          ->order('id DESC')
		          ->limit('10,0');
		         
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	

    public function get_sec($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article') 
		          ->where('cat_id = ?',$id)
		          ->where('published = ?', '1') ;
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	

    public function get_sec_article($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article',array('id','title','introtext','images','cat_id')) 
		          ->where('section_id = ?',$id)
		          ->where('published = ?', '1')
		          ->where('featured = ?', '1') ;
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
	
   public function get_list_details($cat)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article',array('id','title','introtext','created','images','cat_id')) 
		          ->where('cat_id = ?',$cat)
		          ->order('id DESC');
		         
		$result = $db->fetchAll($select);
	    return $result;          
		
	}

	
    public function get_list_broadcast($id)
	{
		$today=date('Y-m-d');
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('broadcast_schedule',array('id','title','hours')) 
		          ->where('channel = ?',$id)
		          ->where('date = ?',$today)
		          ->order('order ASC');
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	

	public function get_truyenhinh()
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('channel',array('id','title')) 
		          ->where('published = ?','1')
		          ->where('type = ?','1')
		          ->order('order DESC');
		         
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
	public function get_phatthanh()
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('channel',array('id','title')) 
		          ->where('published = ?','1')
		          ->where('type = ?','0')
		          ->order('order DESC');
		         
		$result = $db->fetchAll($select);
	    return $result;       
		
	}	

   public function get_phim($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article',array('id','title','introtext','images','cat_id')) 
		          ->where('section_id = ?',$id)
		          ->order('id DESC')
		          ->limit('5,0');
		         
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	


    public function get_cat_phim($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('category', array('id','title')) 
		          ->where('published = ?', '1')
		          ->where('section_id = ?', $id)
		          ->order('order ASC')
		          ->limit('2,0');
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
   
    public function get_advertising($id)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article') 
		          ->where('section_id = ?',$id)
		          ->where('published = ?', '1') ;
		$result = $db->fetchAll($select);
	    return $result;          
		
	}	
	

    public function online()
	{
		$db = Zend_Registry::get('connectDb');
		
		
		$ipAddress = $_SERVER['REMOTE_ADDR'];
		$lastVisit = time();
		$session_start = time();
		$idSession = session_id();
		
		
		
		$select = $db->select()
		          ->from('online',array('count(*) as sl')) 
		          ->where('idSession = ?',$idSession);
		         
		$result = $db->fetchAll($select);
		if ($result['0']['sl']>0)
			{
			     $where ="idSession ='$idSession'";
				 $data1 = array(
			              	'ipAddress'         	=> $ipAddress,
		  	                'lastVisit'	            => $lastVisit,
		                	'session_start'	        => $lastVisit,		  	               
		  			 );
	             $db->update('online', $data1,$where);
				
			
			}
		else 
			{
			   $data1 = array(
			              	'idSession'         	=> $idSession,
		  	                'ipAddress'         	=> $ipAddress,
		  	                'lastVisit'	            => $lastVisit,
		                	'session_start'	        => $lastVisit,		  	               
		  			 );
	           $db->insert('online', $data1);
	
			}
		
        $sessionTime = 15; //th?i gian luu th�ng tin 
       
	    $db->delete('online', "unix_timestamp() - lastVisit >= $sessionTime * 60");
	  
			
	}	
	
	public function statistics()
	{
		$db = Zend_Registry::get('connectDb');
		
		
		$ipAddress = $_SERVER['REMOTE_ADDR'];
		$lastVisit = time();
		$session_start = time();
		$idSession = session_id();
		
		
		
		$select = $db->select()
		          ->from('sessions',array('count(*) as sl')) 
		          ->where('idSession = ?',$idSession);
		         
		$result = $db->fetchAll($select);
		if ($result['0']['sl']>0)
			{
			     $where ="idSession ='$idSession'";
				 $data1 = array(
			              	'ipAddress'         	=> $ipAddress,
		  	                'lastVisit'	            => $lastVisit,
		                	'session_start'	        => $lastVisit,		  	               
		  			 );
	             $db->update('sessions', $data1,$where);
				
			
			}
		else 
			{
			   $data1 = array(
			              	'idSession'         	=> $idSession,
		  	                'ipAddress'         	=> $ipAddress,
		  	                'lastVisit'	            => $lastVisit,
		                	'session_start'	        => $lastVisit,		  	               
		  			 );
	           $db->insert('sessions', $data1);
	
			}
	}	
	
	public function count_online()
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
			          ->from('online',array('count(*) as sl')) ;
		$result = $db->fetchAll($select);
	    return $result;       
	}
	
   public function count_sessions()
	{
		$db = Zend_Registry::get('connectDb');
		
		$select = $db->select()
			          ->from('sessions',array('count(*) as sl')) ;
		$result = $db->fetchAll($select);
	    return $result;       
	}

     public function get_advertising_index()
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('adverting',array('images','link')) 
		          ->where('menu_id = ?','1')
		          ->where('published = ?', '1') 
		          ->where('position = ?', '0') 
		          ->order('id DESC')
		          ->limit('1,0');
		$result = $db->fetchAll($select);
	    return $result; 
	}
	
    public function get_advertising_list_cat($id=0)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('adverting',array('images','link')) 
		          ->where('menu_id = ?',$id)
		          ->where('published = ?', '1') 
		          ->where('position = ?', '0') 
		          ->group('RAND()')
		          ->limit('1,0');
		$result = $db->fetchAll($select);
	    return $result; 
	}
	
	
    public function get_channel()
	{ 
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('channel',array('title','link_name','link_file','images','id')) 
		          ->where('published = ?', '1') 
		          ->where('type = ?', '1') 
		          ->order('id ASC');
		$result = $db->fetchAll($select);
	    return $result; 
	}
	
   public function get_channel_id($id)
	{ 
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('channel',array('title','link_name','link_file','images','id')) 
		          ->where('published = ?', '1') 
		          ->where('id = ?',$id);
		$result = $db->fetchAll($select);
	    return $result; 
	}
	
		public function get_search($key)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('article',array('id','title','introtext','images','cat_id')) 
		          ->where('published = ?','1')
		          ->where('title LIKE "%'.$key.'%"')
		          ->order('id DESC');
		         
		$result = $db->fetchAll($select);
	    return $result;       
		
	}	
	
	
     public function get_contac()
	{ 
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		          ->from('global') 
		          ->where('id = ?',1);
		$result = $db->fetchAll($select);
	    return $result; 
	}
}